package com.augurit.common.dao;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import com.augurit.common.base.dao.BaseDao;
import com.augurit.common.entity.SysMetaColumn;
import com.augurit.common.sysmeta.convert.SysMetaColumnConvertor;
import com.augurit.common.sysmeta.web.form.SysMetaColumnForm;

@Repository
public class SysMetaColumnDao extends BaseDao<SysMetaColumn, Long>{
	/**
	 * 获取form对象
	 * @param id
	 * @return
	 */
	public SysMetaColumnForm getForm(Long id){
		SysMetaColumnForm form = null;
		if(id != null){
			SysMetaColumn entity = this.get(id);
			form = SysMetaColumnConvertor.convertToForm(entity);
		}
		return form;
	}
	
	/**
	 * 保存或者修改form对象
	 *  @param id
	 */
	public void save(SysMetaColumnForm form){
		if(form != null){
			SysMetaColumn entity = null;
			
			//准备VO对象
			if(form.getId() != null)
				entity = this.get(form.getId());
			else
				entity = new SysMetaColumn();
			
			//属性值转换
			SysMetaColumnConvertor.convertToEntity(form,entity);
			
			//保存
			super.save(entity);
			
			//回填ID属性值
			form.setId(entity.getId());
			
		}
	}
	
	/**
	 * 根据tableId获取form对象列表
	 * @param id
	 * @return
	 */
	public List<SysMetaColumnForm> getColumnsByTableId(Long tableId){
		List<SysMetaColumnForm> list = null;
		if(tableId != null){
			String hql = "from SysMetaColumn ps where ps.tableId="+tableId;
			List<SysMetaColumn> entities = this.find(hql);
			if(entities != null && entities.size()>0)
				list = SysMetaColumnConvertor.convertEntityListToFormList(entities);
		}
		return list;
	}
	
	/**
	 * 根据tableId删除form对象
	 * @param tableId
	 */
	public void deleteByTableId(Long tableId){
		if(tableId != null){
			List<SysMetaColumnForm> list = this.getColumnsByTableId(tableId);
			if(list != null && list.size()>0){
				for(SysMetaColumnForm form:list)
					super.delete(form.getId());
			}
		}
	}
	
	/**
	 * 保存数据字段信息
	 * @param sysMetaColumnList
	 * cuigeqiang
	 */
	public void saveSysMetaColumnList(List<SysMetaColumn> sysMetaColumnList){
		for(int i=0;i<sysMetaColumnList.size();i++){
			SysMetaColumn sysMetaColumn=sysMetaColumnList.get(i);
			this.save(sysMetaColumn);
		}
	}
	
	/**
	 * 通过fieldId进行删除
	 * @param formId
	 * cuigeqiang
	 */
	public void deleteByFieldId(String fieldId){
		String sql="DELETE FROM SYS_META_COLUMN WHERE FIELD_ID=?";
		Session session=this.getSession();
		SQLQuery query=session.createSQLQuery(sql);
		query.setString(0, fieldId);
		query.executeUpdate();
	}
	
	/**
	 * 通过表名进行删除
	 * @param tableName
	 * cuigeqiang
	 */
	public void deleteByTableName(String tableName){
		String sql="DELETE FROM SYS_META_COLUMN WHERE TABLE_ID IN (SELECT ID FROM SYS_META_TABLE WHERE TABLE_NAME=?)";
		Session session=this.getSession();
		SQLQuery query=session.createSQLQuery(sql);
		query.setString(0, tableName);
		query.executeUpdate();
	}
	
	/**
	 * 得到某个表下的设计字段
	 * @param tableName
	 * @return
	 * cuigeqiang
	 */
	public List<Map> getFieldsByTableName(String tableName){
		String hql="select fieldId as id,columnName as columnName,columnComments as columnComments,columnLength as columnLength,sqlType as sqlType,defaultValue as defaultValue,columnPrecision as columnPrecision,columnScale as columnScale,dictionary as dictionay,nullable as nullable,isUnique as unique,isIndex as index " +
				" from SysMetaColumn where fieldId is not null and tableName=:tableName";
		Query query=this.getSession().createQuery(hql);
		query.setString("tableName", tableName);
		query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
		return query.list();
	}
	
	/**
	 * 根据FIELDID查询字段信息
	 * @param fieldId
	 * @return
	 */
	public SysMetaColumn getColumnByFieldId(String fieldId){
		String hql="from SysMetaColumn where fieldId=:fieldId";
		return (SysMetaColumn)this.getSession().createQuery(hql).setString("fieldId", fieldId).uniqueResult();
	}
	
}
